﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameEngine
{
    public class DecorationLayer : Layer
    {
        #region Attributes

        public bool XRepeat {set; get;}
        public bool YRepeat {set; get;}
        public List<Sprite> LayerBackground {set; get;}
        public Vector2 ScrolleSpeed {set; get;}
        public Vector2 AutoScrollSpeed { set; get; }
        public Rectangle ScreenBoundings { set; get; }

        #endregion

        #region Constructor and Initialization

        public DecorationLayer(Texture2D layerTexture, Vector2 startLocation, bool xRepeat, bool yRepeat, Vector2 scrolleSpeed, Vector2 autoScrollSpeed, Rectangle screenBoundings, float layerScale)
            : base(layerTexture, startLocation, layerScale)
        {
            XRepeat = xRepeat;
            YRepeat = yRepeat;
            ScrolleSpeed = scrolleSpeed;
            AutoScrollSpeed = autoScrollSpeed;
            ScreenBoundings = screenBoundings;

            LayerBackground = new List<Sprite>();

            initLayerBackground();
        }

        private void initLayerBackground()
        {

            int xSpriteCount = (XRepeat) ? (ScreenBoundings.Width / (int)(LayerTexture.Width * LayerScale)) + 2 : 1;
            int ySpriteCount = (YRepeat) ? (ScreenBoundings.Height / (int)(LayerTexture.Height * LayerScale)) + 2 : 1;

            //Om Repeat
            //Skapa rätt antal element som krävs för att kunna täcka hela bakgrunden 
            //Sätt första elementets position utifrån elementets origin och dess förhållande till spelarens nuvarande position
            //utgå från första elementet och sätt sedan ut alla de andra elementen i förhållande till den.


            for (int x = 0; x < xSpriteCount; x++)
            {
                for (int y = 0; y < ySpriteCount; y++)
                {
                    LayerBackground.Add(
                        new Sprite(
                            new Vector2(StartLocation.X + x * LayerTexture.Width * LayerScale, StartLocation.Y + y * LayerTexture.Height * LayerScale), LayerTexture,
                                new Rectangle(0, 0, LayerTexture.Width, LayerTexture.Height), Vector2.Zero, _layerScale));
                }
            }
        }

        #endregion

        #region Public Methods

        public override void Update(GameTime gameTime, Vector2 playerLocation)
        {

        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            foreach (Sprite backgroundElement in LayerBackground)
            {
                backgroundElement.Draw(spriteBatch);
               
            }
        }
        #endregion

        #region Properties

        public Vector2 Origin
        {
            get
            {
                return (LayerBackground.Count > 0) ?
                    new Vector2(LayerBackground.First().FrameWidth / 2, LayerBackground.First().FrameHeight / 2)
                    : Vector2.Zero;
            }
        }

        #endregion
    }
}
